package com.hyc.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.hyc.util.project.LayUIPage;
import com.hyc.util.project.PageCond;
import com.hyc.util.project.ProjectHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;


@Slf4j
public abstract class BaseController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;

//    protected void responseJson(Object json) {
//        response.setContentType("text/html; charset=utf-8");
//        try {
//            JSONObject obj = (JSONObject) JSON.toJSON(json);
//            response.getWriter().print(obj);
//        } catch (IOException e) {
//            throw new RuntimeException("render json error" + json, e);
//        }
//    }
//
//    protected void responseJsonWithData(Object data, ErrorEnum errorEnum) {
//        response.setContentType("text/html; charset=utf-8");
//        try {
//            RespJson resultJson = new RespJson();
//            resultJson.setCode(errorEnum.getErrorCode());
//            resultJson.setMsg(errorEnum.getMsg());
//            resultJson.setData(data);
//            JSONObject obj = (JSONObject) JSONObject.toJSON(resultJson);
//            response.getWriter().print(obj);
//        } catch (IOException e) {
//            throw new RuntimeException("render json error" + data, e);
//        }
//    }

//    /**
//     * @Description:获取页面参数,通过反射映入实体,json类型适用
//     */
//    protected <T> T getParamJson(Class<T> respClass, boolean check) {
//        if (check) {
//            return getParamJson(respClass);
//        }
//        T resp = null;
//        try {
//            String param = getParamNonNull("param");
//            JSONObject json = JSONObject.parseObject(param);
//            // 入参格式错误
//            if (json == null) {
//                return null;
//            }
//            // 需要赋值的类
//            resp = respClass.newInstance();
//            resp = JSONObject.toJavaObject(json, respClass);
//            log.info("param:" + json.toString());
//        } catch (Exception e) {
//            log.error("调用接口异常" + resp, e);
//            // throw e;
//        }
//        return resp;
//    }
//
//    /**
//     * @Description:获取页面参数,通过反射映入实体,json类型适用
//     */
//    protected <T> T getParamJson(Class<T> respClass) {
//        T resp = null;
//        String param = getParamNonNull("param");
//        JSONObject json = checkParam(param);
//        // 入参格式错误
//        if (json == null) {
//            return null;
//        }
//        try {
//            // 需要赋值的类
//            resp = respClass.newInstance();
//            resp = JSONObject.toJavaObject(json, respClass);
//            log.info("param:" + json.toString());
//
//
//        } catch (Exception e) {
//            log.error("调用接口异常" + resp, e);
//            // throw e;
//        }
//        return resp;
//    }
//
//    /**
//     * @Description:获取页面参数,通过反射映入实体,json类型适用
//     */
//    protected String getParamBase() {
//        String param = getParamNonNull("param");
//        if ("".equals(param)) {
//            return null;
//        }
//        return param;
//    }

    protected void setAttr(String key, Object value){
        request.setAttribute(key, value);
    }

    protected String getParamNonNull(String param) {
        return StringUtil.isEmpty(getParam(param)) ? "" : getParam(param);
    }

    public String getParam(String nm) {
        return request.getParameter(nm);
    }

    protected Byte getByteParamNonNull(String param) {
        try {
            return Byte.parseByte(getParamNonNull(param));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    protected Short getShortParamNonNull(String param) {
        try {
            return Short.parseShort(getParamNonNull(param));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    protected Integer getIntParamNonNull(String param) {
        try {
            return Integer.parseInt(getParamNonNull(param));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    protected Long getLongParamNonNull(String param) {
        try {
            return Long.parseLong(getParamNonNull(param));
        } catch (NumberFormatException e) {
            return 0L;
        }
    }

    protected Double getDoubleParamNonNull(String param) {
        try {
            return Double.parseDouble(getParamNonNull(param));
        } catch (NumberFormatException e) {
            return 0D;
        }
    }

    /**
     * @param param
     * @Description:校验接口入参是否有效
     */
    public JSONObject checkParam(String param) {
        if (StringUtils.isBlank(param)) {
            return null;
        }
        try {
            param = URLDecoder.decode(param, "utf-8");
        } catch (Exception e) {
            log.error("checkParam异常", e);
        }
        JSONObject json = null;
        json = JSONObject.parseObject(param);
        return json;
    }

    protected <T> LayUIPage layUIPage(PageInfo<T> page) {
        LayUIPage layPage = new LayUIPage();
        layPage.setMsg("");
        layPage.setCode("0");
        layPage.setCount((int) page.getTotal());
        layPage.setData(page.getList());

        return layPage;
    }

    protected <T> LayUIPage layUIPage(List<T> page) {
        LayUIPage layPage = new LayUIPage();
        layPage.setMsg("");
        layPage.setCode("0");
        layPage.setData(page);

        return layPage;
    }

    protected <T> LayUIPage layUIPage(List<T> page, int count) {
        LayUIPage layPage = new LayUIPage();
        layPage.setMsg("");
        layPage.setCode("0");
        layPage.setCount(count);
        layPage.setData(page);

        return layPage;
    }

    protected PageCond getPageCond() {
        int page = getIntParamNonNull("page");
        int limit = getIntParamNonNull("limit");
        if (page == 0 && limit == 0) {
            page = 1;
            limit = 10;
        }
        return ProjectHelper.buildPage(page, limit);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }
}
